/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.MainView;
import org.opu.pc.ui.util.Options;

/**
 *
 * @author Yuriy Dunko
 */
public class PublicationsOfProfessors extends AbstractStatementGeneration
        implements PropertyChangeListener {

	private static final long serialVersionUID = 6491098017820778028L;
	
	private int tmpId = -1;
    private boolean canBeAnable = false;

    @Override
    protected boolean isAvailable(List<?> beans, Class<?> clazz) {
        boolean b = beans != null && beans.size() == 1
                && clazz != null && clazz == PublisherBean.class;

        if (!b){
            Options.error(getSafety("PublisherPublicationsAll.err1", "You can "
                    + "perform this action only if you select ONE publisher"));
            return b;
        }

        tmpId = ((PublisherBean)beans.get(0)).getId();

        return b;
    }

    @Override
    protected void generateStatement(File file) throws IOException {
        statementGenerationService.professorScienceStatement(file, tmpId);
    }

    @Override
    public String getName() {
        return "PublicationsOfProfessors";
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List list = (List) evt.getNewValue();
			setEnabled((list != null && list.size() == 1) 
                    && canBeAnable
                    && !((PublisherBean)list.get(0)).isStudent());
		} else if (MainView.CONTENT_TYPE.equals(evt.getPropertyName())){
			canBeAnable = (PublisherBean.class.equals(evt.getNewValue()));
		}
	}
}
